const app = getApp();
var api = require('../../api.js');
var mta = require('../../mta_analysis.js');

//js
Page({
  data: {
    changeState:false,
    lesson:[],
    course:[],
    level:[]
  },
  onLoad: function () {
    var page = this
    app.bind();
    wx.getNetworkType({
      success: function (res) {
        console.log(res)
        page.setData({
          networkType: res.networkType
        })
      }
    });

    mta.Page.init();

   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDetail();
  },

  onHide: function () {
    console.log("onHide");
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var page = this;

    page.getDetail();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },

  onShareAppMessage: function () {
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };
  },

  //获取详情
  getDetail: function () {
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var page = this;
    wx.request({
      url: api.userdd.earn,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          
            page.setData({
              course: res.data.data.course,
              lesson: res.data.data.lesson,
              level: res.data.data.level
            })
            console.log(page.data.course)
            console.log(page.data.lesson)
            console.log(page.data.level)

            if (page.data.course == null && page.data.lesson == null && page.data.level == null ){
                console.log(22222222222222)
                page.setData({
                  changeState:true
                })
            }

            console.log(3333333333)
          
          
        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }
        
      }
    });

  },

  //跳转到专栏
  toCourse: function (e) {
    var page = this;
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var lessonList = page.data.lesson;
    var lessonId = lessonList[index].id;
    var lessonType = lessonList[index].type;
    let str = JSON.stringify(lessonList[index]);
    console.log(str);
    console.log(lessonId);
    if (lessonType == 0){
      wx.navigateTo({
        url: '../../pages/audioDetail/audioDetail?lessonId=' + lessonId,
      })
    }else{
      wx.navigateTo({
        url: '../../pages/classColumn/classColumn?lessonId=' + lessonId,
      })
    }
    
  },

  //跳转到线下课程
  toClass:function(res){
    console.log(res)
    var page = this ;
    var index = res.currentTarget.dataset.index;
    var id = page.data.course[index].id;
    console.log(page.data.course[index].id)
    wx.navigateTo({
      url: '../../pages/courseDetail/courseDetail?id='+id
    })
  }

  

})